home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 25 user and custom controls / customcontrollibrary / navigateribbon.vb < prev    next >
Encoding:
Text File  |  2002-03-19  |  3.3 KB  |  103 lines

  1. ' This control implements a ribbon of four hyperlinks, and can be used
  2. ' to navigate through a number of pages.
  3.  
  4. Imports System.ComponentModel
  5. Imports System.Web.UI
  6.  
  7. <ToolboxData("<{0}:NavigateRibbon runat=server></{0}:NavigateRibbon>")> _
  8. Public Class NavigateRibbon
  9.     Inherits System.Web.UI.WebControls.WebControl
  10.     Implements IPostBackEventHandler
  11.  
  12.     ' The PageCount property.
  13.     <Description("The number of pages")> _
  14.     Property PageCount() As Integer
  15.         Get
  16.             Dim o As Object = Me.ViewState("PageCount")
  17.             If o Is Nothing Then
  18.                 ' return the default PageCount value
  19.                 Return 100
  20.             Else
  21.                 Return CInt(o)
  22.             End If
  23.         End Get
  24.         Set(ByVal Value As Integer)
  25.             Me.ViewState("PageCount") = Value
  26.         End Set
  27.     End Property
  28.  
  29.     ' The PageNumber property.
  30.  
  31.     <Description("The current page number")> _
  32.     Property PageNumber() As Integer
  33.         Get
  34.             Dim o As Object = Me.ViewState("PageNumber")
  35.             If o Is Nothing Then
  36.                 Return 1    ' the default value
  37.             Else
  38.                 Return CInt(o)
  39.             End If
  40.         End Get
  41.         Set(ByVal Value As Integer)
  42.             Me.ViewState("PageNumber") = Value
  43.         End Set
  44.     End Property
  45.  
  46.     ' this is where the actual code is produced
  47.  
  48.     Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
  49.         Dim items() As String = {"First", "Previous", "Next", "Last"}
  50.  
  51.         Dim s As String
  52.         For Each s In items
  53.             ' Create the <A HREF="javascript:__doPostBack(....)"> tag
  54.             output.AddAttribute("id", Me.ClientID)
  55.             output.AddAttribute("href", "javascript:" & Page.GetPostBackEventReference(Me, s))
  56.             output.RenderBeginTag("a")
  57.             ' Display the caption.
  58.             output.Write(s)
  59.             ' Close the tag.
  60.             output.RenderEndTag()
  61.             ' Add a couple of spaces.
  62.             output.Write(" ")
  63.             output.Write(" ")
  64.         Next
  65.     End Sub
  66.  
  67.     ' this method is called when one of the hyperlinks is clicked
  68.  
  69.     Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
  70.         Dim pageNum As Integer = PageNumber
  71.  
  72.         ' update the page number
  73.         Select Case eventArgument.ToLower
  74.             Case "first"
  75.                 pageNum = 1
  76.             Case "previous"
  77.                 pageNum = Math.Max(pageNum - 1, 1)
  78.             Case "next"
  79.                 pageNum = Math.Min(pageNum + 1, PageCount)
  80.             Case "last"
  81.                 pageNum = PageCount
  82.         End Select
  83.  
  84.         ' raise an event in the client if a new page is being shown
  85.         If PageNumber <> pageNum Then
  86.             PageNumber = pageNum
  87.             OnPageChanged(EventArgs.Empty)
  88.         End If
  89.     End Sub
  90.  
  91.     ' this is a public event that takes no extra arguments
  92.     Event PageChanged As EventHandler
  93.  
  94.     ' derived controls should override this method to control
  95.     ' whether the PageChanged event should be send to their clients
  96.  
  97.     Protected Overridable Sub OnPageChanged(ByVal e As EventArgs)
  98.         RaiseEvent PageChanged(Me, e)
  99.     End Sub
  100.  
  101. End Class
  102.  
  103.